package com.gln.bloger.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gln.bloger.compass.SearchResultWapper;
import com.gln.bloger.log.Logging;
import com.gln.bloger.util.StringUtil;

/**
 * entry for user
 * Created on 20080115
 * @author renxj
 * modify entries/articles 
 */
public class SearchAction extends GuestBaseAction {
	/**
	 * search action
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward search(ActionMapping mapping, ActionForm form,
			   HttpServletRequest request, HttpServletResponse response) throws Exception{   
		String p = (String)request.getParameter("p");
		String query = (String) request.getParameter("query");
		int currentPage = 1;
		if(StringUtil.isNumber(p)){
			currentPage = Integer.parseInt(p);
		}
		try{
			SearchResultWapper result = getSearchService().search(query, currentPage);
			if(result.getHits().size() > 0){
				request.setAttribute("preP", currentPage-1);
				request.setAttribute("nextP", currentPage+1);
				request.setAttribute("p", currentPage);
				request.setAttribute("result", result);
				request.setAttribute("searchTime", result.getSearchTime());
			}
		}catch (Exception ex){
			ex.printStackTrace();
			Logging.info(ex.getMessage());
		}
		return mapping.findForward("searchResultView");
	}
}
